#!/bin/bash
base_path=$(cd `dirname $0`; pwd) # 当前目录
inofity_path="/data/website/webtest/" # 监控目录
tmp_deal_file_path="/tmp/downpack/" # 处理后临时存放目录
file_web_path="/data/website/shell/inotifygit/dist/" # 真实保存目录
file_add_path=${base_path}"/source/" # 附件目录
deal_log_path=${base_path}"/log/" # 日志目录
deletefiles=("*.url" "Readme-说明.htm" "Readme-说明.html" "使用说明.txt" "下载使用说明.txt" "使用更新说明.txt" "西西*" "PC6*" "软件说明.txt" "文件说明.txt" "www.cr173.com*" "欢迎访问西西*" "173软件下载.txt" "173下载使用说明.txt")
IFS=$'\n'
cd ${inofity_path} # 进入监控目录
/usr/bin/inotifywait -mrq --format  '%Xe %w%f' -e close_write ./ | while read file # 监控文件close_write（文件写入完成）
do
  INO_EVENT=$(echo $file | awk '{print $1}') # 获取监听状态 下面用作if判断
  INO_FILE=$(echo $file | awk '{print $2}') # 获取发生改变的文件
  old_file_path=$(dirname ${INO_FILE} | awk -F '\\.\\/' '{print $NF}')/ # 获取源文件路径，相对路径
  if [[ ${old_file_path} == "./"  || ${old_file_path} == "." ]]; then # 如果源文件没有目录，就释放这个变量
    unset old_file_path
  fi
  file_name=$(basename ${INO_FILE}) # 获取文件名称，有后缀
  full_file_path=${inofity_path}${old_file_path}${file_name} # 获取源文件绝对路径
  file_ext=$( echo $( echo ${INO_FILE} | awk -F '.' '{print $NF}' ) | tr 'A-Z' 'a-z') # 获取文件后缀

  if [[ ${file_ext} == "zip" || ${file_ext} == "rar" ]]; then
    tmp_dir=${tmp_deal_file_path}${old_file_path}$(basename ${INO_FILE} .${file_ext}) # 因为可能出现文件类型是rar的，但是后缀是zip的，所以，这个地方动态处理
    if [ ! -d ${tmp_dir} ]; then
        mkdir -p ${tmp_dir}
    fi
    file ${full_file_path} | grep 'Zip archive data'
    if [ $? -eq 0 ]; then
      unzip -q -o -O cp936 -P passwork ${full_file_path} -d ${tmp_dir}
      if [ $? != 0 ]; then
        rm -rf ${tmp_dir}
        rm -rf ${full_file_path}
        echo "${full_file_path} unzip error need password or the file is bad zipfile" | tee -a ${deal_log_path}error.log
        continue
      fi
    fi
    file ${full_file_path} | grep 'RAR archive data' >/dev/null
    if [ $? -eq 0 ]; then
      # 增加rar密码文件判断，加密文件以"星号"开头，所以如果发现行首是*开头说明压缩包里面有加密文件。只要有一个加密文件也都不再处理这个rar文件了。
      encryptedfiles=`rar va ${full_file_path} | grep -A1000 '\-\-\-\-\-' | grep -B1000  '\-\-\-\-\-'|grep '^\*'|wc -l`
      if [ ${encryptedfiles} != 0 ]; then
        rm -rf ${full_file_path}
        echo "${full_file_path} rar error need password or the file is bad rarfile" | tee -a ${deal_log_path}error.log
        continue
      fi
      rar e -idp -y ${full_file_path} ${tmp_dir}
      if [ $? != 0 ]; then
        rm -rf ${tmp_dir}
        rm -rf ${full_file_path}
        echo "${full_file_path} unrar error or the file is bad rarfile" | tee -a ${deal_log_path}error.log
        continue
      fi
    fi
    cd ${tmp_dir} # 进入解压目录
    # 删除版权等信息
    for delfile in ${deletefiles[@]};
    do
      `find . -iname ${delfile} -exec rm -rf {} \;` #循环删除数组里面的文件
    done
    cp -arf ${file_add_path}* .
    dist=${tmp_dir}".zip"
    zip -q -r ${dist} *
    cd ${tmp_deal_file_path}${old_file_path}
    zip -z -q ${dist} < ${file_add_path}"memo.txt"
    rm -rf ${tmp_dir}
    rm -rf ${full_file_path}
    # 最后，将处理后的文件移动到正式目录
    if [ ! -d ${file_web_path}${old_file_path} ]; then
      mkdir -p ${file_web_path}${old_file_path}
    fi
    mv -f ${dist} ${file_web_path}${old_file_path}
  else
    # 如果是其他文件，直接复制过去就好
    tmp_dir=${file_web_path}${old_file_path}
    if [ ! -d ${tmp_dir} ]; then
        mkdir -p ${tmp_dir}
    fi
    mv -f ${full_file_path} ${tmp_dir}
  fi
done